const express = require("express");
const app = express();
const port = 3000;

const mysql = require('mysql');
let connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'root',
  port: '3306',
  database: 'test'
});
connection.connect();

app.get("/", (req, res) => {


  let sql = 'SELECT * FROM test';
  connection.query(sql, function (err, result) {
    if (err) {
      console.log('[SELECT ERROR] - ', err.message);
      res.status(500).send('数据库查询错误');
      return;
    }

    console.log('--------------------------SELECT----------------------------');
    console.log(result);
    console.log('------------------------------------------------------------\n\n');
    
    // 将查询结果发送给客户端
    res.json(result);
  });

 
});



// app.get("/api/users", (req, res) => {
//     res.json({ id: 1, name: "John Doe", email: "john@doe.com" });
// });

// app.get("/api/users/:id", (req, res) => {
//     res.json({ id: req.params.id, name: "John Doe" + req.params.id, email: "john@doe.com" });
// });

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
